package com.example.spinnertest;

import androidx.annotation.LongDef;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
// 功能：显示spinner，及选中加入数据库
public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    // 获取数据
    String[] city=new String[]{"学习", "理财", "随便" , "时间"};
    // Note
    Note note = new Note();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = findViewById(R.id.spinner);
        //设置下拉框默认显示第一项
        spinner.setSelection(0);
        ArrayAdapter<String> spadapter=new
                ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,city);
        spinner.setAdapter(spadapter);
        //设置选择监听器
        spinner.setOnItemSelectedListener(new MySelectedListener());
        String f = spinner.getSelectedItem().toString();
        Log.d("test", "onCreate: " + f);
    }

    class MySelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Toast.makeText(MainActivity.this, "您选择的是"+ city[arg2], Toast.LENGTH_LONG).show();
            // 选中的tag
            String tag = city[arg2];.
            Note note = new Note();
            note.setContent("哈哈哈");
            note.setId(1);
            note.setTag(tag);
            CRUD.save(note);
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }

}